package com.vmix.android.vmixsdk.rds;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.vmix.android.vmixsdk.config.VmixConfig;
import com.vmix.android.vmixsdk.exception.VmixException;
import com.vmix.android.vmixsdk.valueobject.VmixVideo;
import com.vmix.android.vmixsdk.valueobject.VmixVideos;

/**
 * GetMediaList operations
 * 
 * @author lei
 *
 */
public class GetMediaListOperation extends AbstractOperation {
	
	private final static String ACTION = "getMediaList";
	
	protected String collectionId;
	
	/**
	 * constructor
	 */
	public GetMediaListOperation( String collectionId ) {
		
		// define a service url
		this.serviceUrl = VmixConfig.MEDIA;
	
		this.collectionId = collectionId;
	}
	
	/**
	 * process to return a list of vmix videos
	 * @return
	 */
	public VmixVideos process() throws VmixException {
		
		// build out the request url
		this.serviceUrl += "action=" + this.ACTION + "&collection_id="+ this.collectionId 
			+ "&atoken="+ VmixConfig.ATOKEN+"&output=xml";
		
		VmixVideos videos = new VmixVideos();
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

		try {
			
			DocumentBuilder builder = factory.newDocumentBuilder();
			
			Document dom = builder.parse( this.serviceUrl );
			Element root = dom.getDocumentElement();
			NodeList items = root.getElementsByTagName( "media" );
			
			for( int i = 0; i < items.getLength(); i++ ) {

				VmixVideo video = new VmixVideo();
				
				// get media item
				Node mediaItem = items.item(i);
				NodeList properties = mediaItem.getChildNodes();
				
				// get a list of the childnodes
				for( int j = 0; j < properties.getLength(); j++ ) {
					
					// pull out property name and value
					Node property = properties.item(j);
					String propertyName = property.getNodeName();
					String propertyValue = property.getNodeValue();
					
					// set id
					if( propertyName.equalsIgnoreCase("id") ) {
						video.setMediaId(propertyValue);
					}
					// set partner id
					else if( propertyName.equalsIgnoreCase("partner_id") ) {
						video.setPartnerId(propertyValue);
					}
					// set title
					else if( propertyName.equalsIgnoreCase("title")) {
						video.setTitle(propertyValue);
					}
					// set description
					else if( propertyName.equalsIgnoreCase("description") ) {
						video.setDescription(propertyValue);
					}
					// set class_id
					else if( propertyName.equalsIgnoreCase("class_id") ) {
						video.setClassId(propertyValue);
					}
					// set date created
					else if( propertyName.equalsIgnoreCase("date_created") ) {
						video.setDateCreated(propertyValue);
					}
					// set date approved
					else if( propertyName.equalsIgnoreCase("date_approved") ) {
						video.setDateApproved(propertyValue);
					}
					// set last modified
					else if( propertyName.equalsIgnoreCase("date_modified") ) {
						video.setLastModified(propertyValue);
					}
					// set source name
					else if( propertyName.equalsIgnoreCase("source_name") ) {
						video.setSourceName(propertyValue);
					}
					// set total views
					else if( propertyName.equalsIgnoreCase("total_views") ) {
						video.setTotalViews(propertyValue);
					}

				}
				
				videos.add( video );
			}
			
			return videos;
		}
		catch( Exception e ) {
			
			throw new VmixException( e.getMessage() );
		}

	}

}
